package ui
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import util.Util;

	/**
	 * 列表控件
	 * @author jianhong
	 */ 
	public class List extends Sprite implements IScrollView
	{
		private		var	_width:int = 0;
		private		var _height:int = 0;
		
		private		const MARGIN_WIDTH:int = 1;
		private		const MARGIN_HEIGHT:int = 1;
		private		const FIELD_GAP:int = 0;
		private		const FIELD_HEIGHT:int = 23;
		
		private		var _fieldLayer:Sprite = null;
		
		private		var _fieldNum:int = 0;
		private		var _fieldNumMin:int = 0;
		
		private		var _arrItem:Array = new Array();
		
		private		var _propertyInvalidate:Boolean = false;
		
		private		var	_selectedFieldItem:TextField = null;
		private		var _selectedColorBk:uint = 0;
		
		private		var	_formatFun:Function = null;
		
		public function List(width:int, height:int)
		{
			super();
			
			this._width = width;
			this._height = height;
			
			this.graphics.beginFill(0xC2D6E8);
			this.graphics.drawRect(0, 0, this._width, this._height);
			this.graphics.endFill();
			
			this._fieldLayer = new Sprite();
			this._fieldLayer.graphics.beginFill(0xffffff);
			this._fieldLayer.graphics.drawRect(0, 0, this._width - this.MARGIN_WIDTH*2, this._height-this.MARGIN_HEIGHT*2);
			this._fieldLayer.graphics.endFill();
			
			var lastBottom:int = 0;
			this._fieldNumMin = Math.ceil(this._height/(this.FIELD_HEIGHT+this.FIELD_GAP));
			this.addFieldItem(this._fieldNumMin);
			var sRect:Rectangle = new Rectangle(0, 0, this._width-this.MARGIN_WIDTH*2, this._height-this.MARGIN_HEIGHT*2);
			this._fieldLayer.scrollRect = sRect;
			this._fieldLayer.x = this.MARGIN_WIDTH;
			this._fieldLayer.y = this.MARGIN_HEIGHT;
			this.addChild(this._fieldLayer);
			this.doubleClickEnabled = true;
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			this.addEventListener(MouseEvent.CLICK, onClick);
			this.addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClick);
		}

		public	function	onDoubleClick(evt:MouseEvent):void{
			var textField:TextField = evt.target as TextField;
			if (textField == null)
				return;
			
			if (textField.text == null || textField.text == "")
				return;
			
			if (this._selectedFieldItem != null){
				this._selectedFieldItem.backgroundColor = this._selectedColorBk;
				this._selectedFieldItem.textColor = 0x444B50;
			}
			
			this._selectedColorBk = textField.backgroundColor;
			this._selectedFieldItem = textField;
			
			textField.backgroundColor = 0xd9e2ea;
			textField.textColor = 0xffffff;
			
			var sel:int = this.getSelectedIndex();
			var item:ListItem = this.getListItemFromIndex(sel);
			var playEvt:EventPlayCmd = new EventPlayCmd();
			playEvt.name = item.name;
			playEvt.url = item.url;
			playEvt.artist = item.artist;
			this.dispatchEvent(playEvt);
		}
		
		public	function	onClick(evt:MouseEvent):void{
			var textField:TextField = evt.target as TextField;
			if (textField == null)
				return;
			
			if (textField.text == null || textField.text == "")
				return;
			
			if (this._selectedFieldItem != null){
				this._selectedFieldItem.backgroundColor = this._selectedColorBk;
				this._selectedFieldItem.textColor = 0x444B50;
			}
			
			this._selectedColorBk = textField.backgroundColor;
			this._selectedFieldItem = textField;
			
			textField.backgroundColor = 0xd9e2ea;
			textField.textColor = 0xffffff;
		}
		
		public	function	onEnterFrame(evt:Event):void{
			if (this._propertyInvalidate == true){
				this.renderItem();
				this._propertyInvalidate = false;
			}
		}
		
		/**
		 * 滚动列表
		 */ 
		public	function	scrollV(cy:int):void{
			var rect:Rectangle = this._fieldLayer.scrollRect;
			rect.y += cy;
			if (rect.y < 0){
				rect.y = 0;
			}
			else if(rect.y > this.documentHeight - this.viewHeight){
				rect.y = this.documentHeight-this.viewHeight;
			}
			this._fieldLayer.scrollRect = rect;
		}
		
		/**
		 * 设置视图位置
		 */ 
		public	function	setViewPos(y:int):void{
			var rect:Rectangle = this._fieldLayer.scrollRect;
			rect.y =y;
			if (rect.y < 0){
				rect.y = 0;
			}
			else if(rect.y > this.documentHeight - this.viewHeight){
				rect.y = this.documentHeight-this.viewHeight;
			}
			this._fieldLayer.scrollRect = rect;
		}
		
		/**
		 * 列表视口高度
		 */ 
		public	function	get viewHeight():int{
//			var rect:Rectangle = this._fieldLayer.scrollRect;
//			return rect.height;
			return this.height;
		}
		
		/**
		 * 列表实际高度
		 */ 
		public	function	get documentHeight():int{
			var value:int = (this.FIELD_HEIGHT+this.FIELD_GAP)*this.itemNum;
			if (value < this.viewHeight){
				value = this.viewHeight;
			}
			return value;
		}
		
		/**
		 * 列表项数量
		 */ 
		public	function	get itemNum():int{
			return this._arrItem.length;
		}
		
		/**
		 * 覆盖高度计算
		 */ 
		public	override function	get	height():Number{
			return this._height;
		}
		
		private	function	addFieldItem(num:int):void{
			var lastBottom:int = 0;
			var fieldItemCount:int = this._fieldLayer.numChildren;
			if (fieldItemCount > 0){
				var textField:TextField = this._fieldLayer.getChildAt(fieldItemCount-1) as TextField;
				lastBottom = textField.y+textField.height;
			}
			
			for(var i:int = 0; i < num; i++){
				var field:TextField = new TextField();		
				field.autoSize = TextFieldAutoSize.NONE;
				field.x = 0;
				field.y = lastBottom + this.FIELD_GAP;
				field.width = this._width-this.MARGIN_WIDTH*2;
				field.height = this.FIELD_HEIGHT;
				field.background = true;
				field.selectable = false;
				field.doubleClickEnabled = true;
				field.textColor = 0x444B50;
				if (this._fieldNum%2 == 0){
					field.backgroundColor = 0xffffff;
				}
				else{
					field.backgroundColor = 0xF0F8FF;
				}
				lastBottom = field.y + field.height;
				
				var format:TextFormat = new TextFormat();
				format.leftMargin = 5;
				field.setTextFormat(format);
				this._fieldLayer.addChild(field);
				this._fieldNum++;
			}
		}
		
		private	function	removeFieldItem(num:int):void{
			var removeNum:int = this._fieldNum - this._fieldNumMin;
			if (num < removeNum){
				removeNum = num;
			}
			
			for(var i:int = 0; i < removeNum; i++){
				this._fieldLayer.removeChildAt(this._fieldLayer.numChildren-1);
			}
			
			this._fieldNum -= removeNum;
		}
		
		/**
		 * 添加一个列表项
		 * @param item 列表项实体
		 * @param pos 列表项添加的位置，默认值-1，添加到末尾
		 */ 
		public	function	addItem(item:ListItem, pos:int = -1):void{
			if (pos < 0){
				pos = this._arrItem.length;
			}
			item.name = Util.trim(item.name);
			item.artist = Util.trim(item.artist);
			this._arrItem.splice(pos, 0, item);
			this._propertyInvalidate = true;
		}
		
		/**
		 * 根据位置删除一个列表项
		 */ 
		public	function	removeItem(pos:int):void{
			if (pos < 0){
				pos = -1;
			}
			this._arrItem.splice(pos,1);
			this._propertyInvalidate = true;
		}
		
		/**
		 * 渲染列表项
		 */ 
		private	function		renderItem():void{
			if (this._arrItem.length > this._fieldNum){
				this.addFieldItem(this._arrItem.length - this._fieldNum);
			}
			
			if (this._arrItem.length < this._fieldNum){
				this.removeFieldItem(this._fieldNum - this._arrItem.length);
			}

			for(var i:int = 0; i < this._fieldNum; i++){
				var field:TextField = this._fieldLayer.getChildAt(i) as TextField;
				if (field != null && i < this._arrItem.length){
					var num:int = i+1;
					if (this._formatFun == null){
						field.text = " "+num+"."+this._arrItem[i].name+" - "+this._arrItem[i].artist;
					}
					else{
						field.text = this._formatFun(field, this._arrItem[i], num+".");
					}
				}
				else{
					field.text = "";
				}
			}
			
			var evt:ScrollViewAlterEvent = new ScrollViewAlterEvent();
			evt.viewHeight = this.viewHeight;
			evt.documentHeight = this.documentHeight;
			this.dispatchEvent(evt);
		}
		
		
		/**
		 * 返回选中项的序列号
		 */ 
		public	function	getSelectedIndex():int{
			if (this._selectedFieldItem == null)
				return -1;
			
			var index:int = this._fieldLayer.getChildIndex(this._selectedFieldItem);
			return index;
		}
		
		/**
		 * 根据下标返回ListItem
		 */ 
		public	function	getListItemFromIndex(index:int):ListItem{
			if (this._arrItem.length <= index || index < 0)
				return null;
			return this._arrItem[index];
		}
		
		/**
		 * 根据名字返回序列号
		 * 找不到返回-1
		 */ 
		public	function	getIndexFromName(name:String):int{
			for(var i:int = 0; i < this._arrItem.length; i++){
				if (this._arrItem[i].name == name){
					return i;
				}
			}
			
			return -1;
		}
		
		/**
		 * 设置格式化函数
		 */ 
		public	function	set formatFun(fun:Function):void{
			this._formatFun = fun;
		}
		
		
		/**
		 * 返回格式化函数
		 */ 
		public	function	get formatFun():Function{
			return this._formatFun;
		}
		
		/**
		 * 返回列表项数组
		 */ 
		public	function	getItemList():Array{
			return this._arrItem;
		}
	}
}